<template>
  <div class="app-container">
    <!-- 表格-条件筛区域  -->
    <div class="filter-container">
      <!--<el-input v-model="searchParamKey.SEARCH_LIKE_spec_name" placeholder="请输入关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilterKey" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilterKey">搜索</el-button>-->
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreateKey">添加</el-button>
    </div>
    <el-table :key="tableKey" v-loading="listLoadingKey" :data="tableListKey" border fit highlight-current-row style="width: 100%;height: 250px">
      <el-table-column
        v-for="column in columnKeys"
        :key="column.prop"
        :prop="column.prop"
        :label="column.label"
        :width="column.width"
        :align="column.align?column.align:'center'"
        :min-width="column.minWidth"
        :sortable="column.sortable"
        :fixed="column.fixed"
        :type="column.type"
        :formatter="column.formatter"
      >
        <template slot-scope="scope">
          <img v-if="column.imgFlag==true" :src="CMBASE.baseFileContext + scope.row[column.prop]" width="60" height="60" class="img">
          <!-- 行内显示开关按钮 -->
          <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]" :active-value="1" :inactive-value="0" disabled />
          <!-- 行内显示tag标签 -->
          <span v-else-if="column.tagFlag==true">
            <span v-for="item in column.tagFormatter" :key="item.value">
              <el-tag v-if="item.value==scope.row[column.prop]" :type="item.type">{{ item.text }}</el-tag>
            </span>
          </span>
          <span v-else-if="column.formatter"> {{ column.formatter(scope.row) }}</span>
          <span v-else>{{ scope.row[column.prop] }}</span>
        </template>
      </el-table-column>
      <!-- 操作-->
      <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-show="scope.row.specType != 3" type="info" style="width:90px" @click="viewSpecValueList(scope.row.id,'')">参数值列表</el-button>
          <el-button type="primary" @click="handleUpdateKey(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDeleteKey(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格-页脚区域 -->
    <pagination v-show="tableTotal>0" :total="tableTotal" :page.sync="pageRequest.pageNum" :limit.sync="pageRequest.pageSize" @pagination="getList" />

    <!--新增编辑 spu规格组对应key界面-->
    <el-dialog :title="operationKey?'新增':'编辑'" :visible.sync="dialogFormVisibleKey" :modal="false" :close-on-click-modal="false" width="45%" top="15vh">
      <div>
        <el-form ref="dataKeyForm" :model="dataKeyForm" :rules="dataKeyFormRules" label-width="140px" style="width: 80%">
          <el-form-item label="规格名" prop="specName">
            <el-input v-model="dataKeyForm.specName" auto-complete="off" />
          </el-form-item>
          <el-form-item label="规格限制类型" prop="specType">
            <el-radio-group v-model="dataKeyForm.specType">
              <el-radio label="1">单选</el-radio>
              <el-radio label="2">多选</el-radio>
              <el-radio label="3">手动输入</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="显示位置" prop="showPosition">
            <el-input v-model="dataKeyForm.showPosition" auto-complete="off" />
          </el-form-item>
          <el-form-item label="排序号码" prop="orderNo">
            <el-input v-model="dataKeyForm.orderNo" auto-complete="off" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataKeyForm.remark" auto-complete="off" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeDialogKey()">取消</el-button>
        <el-button :loading="editLoadingKey" size="mini" type="primary" @click.native="saveFormDataKey()">提交
        </el-button>
      </div>
    </el-dialog>

    <!--spu规格value列表弹窗-->
    <el-dialog title="参数Value列表" :visible.sync="getValueListVisible" :modal-append-to-body="false" :modal="false" :close-on-click-modal="false" width="60%" top="10vh">
      <div v-if="getValueListVisible"  style="height:430px">
        <spec-group-value ref="specGroupValue" :row-group-key-id="rowGroupKeyId" style="padding-top: 0px" />
      </div>
    </el-dialog>
  </div>
</template>
<script>

import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination/index' // Secondary package based on el-pagination
import { getMSpecKeyList, saveMSpecKeyInfo, deleteMSpecKeyInfo } from '@/api/modules/goodsInfo'
import specGroupValue from '@/views/goodsattr/specGroupValue'
export default {
  name: 'ProductBrand',
  components: { Pagination, specGroupValue },
  directives: { waves },
  filters: {},
  props: {
    rowGroupId: {
      type: [String],
      default: ''
    }
  },
  data() {
    return {
      tableKey: 0,
      pageRequest: { pageNum: 1, pageSize: 10 },
      searchParamKey: { SEARCH_LIKE_spec_group_id: '', SEARCH_LIKE_spec_name: '' },

      tableListKey: [], // 规格组key表格数据源
      tableTotal: 0, // 表格总条数数据
      listLoadingKey: true,
      treeTableDataListKey: [],
      keyListName: '参数值列表',
      getValueListVisible: false, // 默认不显示规格值弹出框
      columnKeys: [
        // { prop: 'id', label: '主键', sortable: false },
        { prop: 'specName', label: '规格名', sortable: false },
        { prop: 'specType', label: '规格限制类型', sortable: false, formatter: this.formatSpecType },
        { prop: 'showPosition', label: '显示位置', sortable: false },
        { prop: 'orderNo', label: '排序号码', sortable: false },
        { prop: 'createTime', label: '创建时间', sortable: false }
      ],
      fileList: [],
      operationKey: true,
      dialogFormVisibleKey: false,
      // 新增编辑 Value界面数据
      dataKeyForm: {},

      // Value表单检验规则
      dataKeyFormRules: {
        specName: [{ required: true, message: '请输入规格名', trigger: 'blur' }],
        specType: [{ required: true, message: '请选规格限制类型', trigger: 'blur' }],
        showPosition: [{ required: true, message: '请输入显示位置', trigger: 'blur' }],
        searchFlag: [{ required: true, message: '请输入', trigger: 'blur' }],
        orderNo: [{ required: true, message: '请输入排序号码', trigger: 'blur' }],
        remark: [{ required: false, message: '请输入备注', trigger: 'blur' }]
      },
      editLoadingKey: false, // 保存表单时loading框
      props: {
        multiple: false, // 是否多选
        label: 'name',
        value: 'id',
        children: 'children'
      },
      rowGroupKeyId: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(id, value) {
      this.listLoadingKey = true
      // 【API请求】----获取用户列表数据
      this.searchParamKey.SEARCH_LIKE_spec_group_id = this.rowGroupId
      this.pageRequest.searchParam = this.searchParamKey
      getMSpecKeyList(this.pageRequest).then(response => {
        this.tableListKey = response.result.content
        this.tableTotal = response.result.total
        this.listLoadingKey = false
      }).catch((res) => {
        this.$message({ message: '列表获取失败', type: 'error' })
      })
    },

    // 属性值表格数据筛选
    handleFilterKey() {
      var id = this.searchParamKey.SEARCH_LIKE_spec_group_id
      var value = this.searchParamKey.SEARCH_LIKE_spec_name
      this.getList(id, value)
    },

    // 【属性值列表 按钮点击】--- 弹出新增表单
    handleCreateKey() {
      this.operationKey = true
      this.initDataKeyForm()
      this.dataKeyForm.specGroupId = this.searchParamKey.SEARCH_LIKE_spec_group_id
      this.dialogFormVisibleKey = true
      this.$nextTick(() => {
        this.$refs['dataKeyForm'].clearValidate()
        this.editLoadingKey = false
      })
    },
    // 【属性值列表 按钮点击】--- 弹出编辑表单
    handleUpdateKey(row) {
      this.operationKey = false
      this.dataKeyForm = Object.assign({}, row)
      this.dataKeyForm.specType = row.specType + ''
      // 显示编辑菜单
      this.dialogFormVisibleKey = true
      this.$nextTick(() => {
        this.$refs['dataKeyForm'].clearValidate()
        this.editLoadingKey = false
      })
    },
    // 【属性值列表 按钮点击】--- 删除
    handleDeleteKey(row) {
      this.$confirm('确认删除该记录吗？', '提示', {}).then(() => {
        // 【API请求】---- 删除数据
        deleteMSpecKeyInfo({ ids: row.id }).then(() => {
          this.$message({ message: '操作成功', type: 'success' })
          var id = this.searchParamKey.SEARCH_LIKE_spec_group_id
          this.getList(id, '')// 重新获取表格数据
        })
      }).catch(() => {
      })
    },
    // 属性值 保存表单数据
    saveFormDataKey() {
      this.$refs['dataKeyForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.editLoadingKey = true
        // 【API请求】----保存数据
        saveMSpecKeyInfo(this.dataKeyForm).then(() => {
          this.editLoadingKey = false// 关闭loading框
          this.dialogFormVisibleKey = false// 关闭表单页面
          this.initDataKeyForm()// 重置表单数据
          var id = this.searchParamKey.SEARCH_LIKE_spec_group_id
          this.getList(id, '')// 重新获取表格数据
          this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
        })
      })
    },
    // 关闭表单弹出框
    closeDialogKey() {
      this.$refs['dataKeyForm'].resetFields()
      this.dialogFormVisibleKey = false
    },
    initDataKeyForm() {
      this.dataKeyForm = {
        id: '',
        specGroupId: '',
        specName: '',
        specType: '',
        showPosition: '',
        searchFlag: '',
        orderNo: '',
        remark: ''
      }
      Object.assign(this.$data.dataKeyForm, this.$options.data().dataKeyForm)
      this.fileList = []
    },

    handlePreview(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/psd' || file.type === 'image/pdd'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传图片只能是jpeg/png/psd/pdd/ 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },

    viewSpecValueList(id, value) {
      console.log(id)
      this.getValueListVisible = true
      this.rowGroupKeyId = id
    },
    // 格式化 格式限制类型
    formatSpecType(row) {
      if (row.specType === 1) {
        return '单选'
      } else if (row.specType === 2) {
        return '多选'
      } else if (row.specType === 3) {
        return '手动输入'
      } else {
        return '未知'
      }
    }

  }
}
</script>
